﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using System.Resources;

namespace University.Workshop.WinApp
{
    public partial class ModifyProductForm : TemplateForm
    {
        private ResourceManager resours;
        public ModifyProductForm()
        {
            InitializeComponent();
            resours = new ResourceManager(typeof(RegisterAdministrator));
            dataGridViewProduct.Rows.Clear();
            RegistrationProduct.GetAllProduct(dataGridViewProduct);
        }

        private void bGo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void bSearch_Click(object sender, EventArgs e)
        {
            DataSet product = new DataSet();
            string searchProduct = textBoxSearch.Text;
            ProductRegister searchProducts = new ProductRegister();
            dataGridViewProduct.Rows.Clear();
            DataTable table = searchProducts.SeekProduct(searchProduct);
            int countRows = table.Rows.Count;
            for(int i = 0; i < countRows; i++)
            {
                int test= dataGridViewProduct.Rows.Add();
                dataGridViewProduct.Rows[i].Cells[0].Value=table.Rows[i][0];
                dataGridViewProduct.Rows[i].Cells[1].Value=table.Rows[i][1];
                dataGridViewProduct.Rows[i].Cells[2].Value=table.Rows[i][2];
                dataGridViewProduct.Rows[i].Cells[3].Value=table.Rows[i][3];
                dataGridViewProduct.Rows[i].Cells[4].Value = table.Rows[i][4];
                dataGridViewProduct.Rows[i].Cells[5].Value = table.Rows[i][5];
                dataGridViewProduct.Rows[i].Cells[6].Value = table.Rows[i][6];

            }

        }
        private void modifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!(dataGridViewProduct.Rows.Count - 1 > 0) && !(dataGridViewProduct.SelectedRows.Count > 1))
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "rows select is invalid", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                DataGridViewCell selectProduct = dataGridViewProduct.CurrentRow.Cells[0];
                string productSelect = selectProduct.Value.ToString();
                try
                {
                    RegisterProductForm register = new RegisterProductForm(productSelect);
                    register.ShowDialog();
                }
                catch (ArgumentException ex)
                {
                    string message = ex.Message;
                    string caption = this.resours.GetString("messageBox.Caption");
                    MessageBoxButtons buttons = MessageBoxButtons.OK;
                    MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
                }
                ModifyProductForm_Load(sender, e);
            }

        } 

        private void ModifyProductForm_Load(object sender, EventArgs e)
        {
            dataGridViewProduct.Rows.Clear();
            RegistrationProduct.GetAllProduct(dataGridViewProduct);
        }

        private void eraseToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Desea eliminar", "Confirmacion", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if (res.Equals(DialogResult.OK))
            {
                DataGridViewCell selectProduct = dataGridViewProduct.CurrentRow.Cells[0];
                string idProductSelect = selectProduct.Value.ToString();
                ProductRegister productDelete = new ProductRegister();
                productDelete.DeleteProduct(idProductSelect);

            }
            else
            {
                this.Close();
            }   
        }
    }
}
